package com.learn.shop.service.impl;

import com.learn.shop.pojo.entriy.NovelChapterMongo;
import com.learn.shop.pojo.vo.CrawlingNovelVo;
import com.learn.shop.service.CrawlingNovelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Objects;

@Service("CrawlingNovelImpl")
@Slf4j
public class CrawlingNovelImpl implements CrawlingNovelService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String novel(CrawlingNovelVo crawlingNovelV) {

        String url = crawlingNovelV.getUrl();
        String novelName = crawlingNovelV.getNovelName();
        String urlSplit = crawlingNovelV.getUrlSplit();
        String parentUrl = null;
        if (StringUtils.isNotBlank(urlSplit)) {
            parentUrl = url.split(urlSplit)[0] + urlSplit;
        } else {
            parentUrl = url;
        }
        try {
            Document doc = Jsoup.connect(url).get();
            Elements list = Objects.requireNonNull(doc.getElementById("list")).select("dd>a");
            int size = list.size();
//            res = size - 9;
            for (int i = 0; i < list.size(); i++) {
                Element element = list.get(i);
                String childUrl = element.attr("href");
                String startStr = childUrl.substring(0, 1);
                if (!"/".equals(startStr)) {
                    childUrl = "/" + childUrl;
                }
                String title = element.text();
                log.info(title);
                getNovelContent(title, parentUrl, childUrl, i, novelName);
            }
            return "解析成功!->" + size + "章";
        } catch (IOException e) {
            e.printStackTrace();
            return "解析报错!";
        }

    }

    public void getNovelContent(String title, String parentUrl, String childUrl, int id, String novelName) {

        try {
            Document document = Jsoup.connect(parentUrl + childUrl)
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Cache-Control", "no-cache")
                    .header("Connection", "keep-alive")
                    .header("Pragma", "no-cache")
                    .header("Upgrade-Insecure-Requests", "1")
                    .timeout(5000)
                    .userAgent("Mozilla")//模拟浏览器
                    .get();
            String content = Objects.requireNonNull(document.getElementById("content")).html();
            content = content.replace("<br>", "");
            content = content.replace("&nbsp;", "");
            NovelChapterMongo mongo = new NovelChapterMongo();
            mongo.set_id(id + "");
            mongo.setChapterName(title);
            mongo.setText(content);
            mongoTemplate.save(mongo, novelName);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("解析小说失败!");
        }
    }


}
